<?php 
defined('LMO') or die('Can\'t access !');
class LCAccount {
    static $instance;
    
    public static function getInstance()
    {
        if(!isset(self::$instance)) {
            self::$instance = new LCAccount();
        }
        
        return self::$instance;
    }
    
    public function checkPermission()
    {
        if(!isset($_SESSION['logged'])) {
            header('location: '.BASE_URL.BS.'index.php?c=user&m=login');
        }
    }

    public function indexAction() 
    {
        $this->listAction();
    }
    
    public function listAction()
    {
        $this->checkPermission();
        $account = LAccount::getInstance();
        $list = LFile::getListFile(SERVER_STORAGE_PATH, SERVER_FILE_EXT);
        $data = array();
        foreach($list as $ip) {
            $ip = str_replace('.conf', '', $ip);
            $data[] = $account->loadAccount($ip);
        }
        
        $layout = LLayout::getInstance();
        $html = $layout->assign('account_list', array('data' => $data));
        $layout->loadLayout($html);
    }
    
    public function addAction()
    {
        $this->checkPermission();
        if(!empty($_POST)) {
            $params = $_POST;
            $account = LAccount::getInstance();
            if(!isset($params['server_name'])) {
                header('location: '.BASE_URL.BS.'index.php?c=message&type=error&s='.'Please enter Server name !');
                exit;
            }
            if(!isset($params['server_ip'])) {
                header('location: '.BASE_URL.BS.'index.php?c=message&type=error&s='.'Please enter IP !');
                exit;
            }
            //print_r($account->checkExist($params['server_ip']));
            if ($account->checkExist($params['server_ip'])) {
                header('location: '.BASE_URL.BS.'index.php?c=message&type=error&s='.'Account existed !');
                exit;
            }
            
            $data = array();
            $data['name'] = $params['server_name'];
            $data['ip'] = $params['server_ip'];
            //print_r($params);
            //exit;
            if(isset($params['service_name']) && !empty($params['service_name'])) {
                $number = count($params['service_name']);
                for($i = 0; $i < $number; $i++) {
                    if(isset($params['service_name'][$i]) 
                            && isset($params['service_ip'][$i]) 
                            && isset($params['service_port'][$i])
                            && isset($params['service_timeout'][$i])
                            && isset($params['service_alert'][$i])
                            && (strlen($params['service_name'][$i]) > 0)
                            && (strlen($params['service_ip'][$i]) > 0)
                    ) {
                        $service = array(
                            'name' => $params['service_name'][$i],
                            'ip' => $params['service_ip'][$i],
                            'port' => $params['service_port'][$i],
                            'timeout' => ((int)$params['service_timeout'][$i] > 0)?$params['service_timeout'][$i]:5,
                            'alert' => $params['service_alert'][$i],
                            'token' => isset($params['service_token'][$i])?$params['service_token'][$i]:'',
                            'enable' => (isset($params['service_enable'][$i]) && $params['service_enable'][$i] == 1)?true:false,
                        );
                    }
                    $data['service'][] = $service;
                }
            }
            //print_r($data); exit;
            $account = LAccount::getInstance();
            $account->saveAccount($data);
            header('location: '.BASE_URL.BS.'index.php?c=message&type=ok&s='.'Account added !');
        }
        
        $layout = LLayout::getInstance();
        $html = $layout->assign('account_add');
        $layout->loadLayout($html);
    }
    
    public function editAction()
    {
        $this->checkPermission();
        $ip = isset($_GET['ip'])?$_GET['ip']:null;
        
        $account = LAccount::getInstance();
        if (!$account->checkExist($ip)) {
            header('location: '.BASE_URL.BS.'index.php?c=message&type=error&s='.'Account doesn\'t exist !');
            exit;
        }
        
        if(!empty($_POST)) {
            $params = $_POST;
            if(!isset($params['server_name'])) {
                header('location: '.BASE_URL.BS.'index.php?c=message&type=error&s='.'Please enter Server name !');
                exit;
            }
            if(!isset($params['server_ip'])) {
                header('location: '.BASE_URL.BS.'index.php?c=message&type=error&s='.'Please enter IP !');
                exit;
            }
            //print_r($account->checkExist($params['server_ip']));
            
            $data = array();
            $data['name'] = $params['server_name'];
            $data['ip'] = $params['server_ip'];
            //print_r($params); exit;
            if(isset($params['i'])) {
                $number = $params['i'];
                for($i = 0; $i <= $number; $i++) {
                    if(isset($params['service_name_'.$i]) 
                            && isset($params['service_ip_'.$i]) 
                            && isset($params['service_port_'.$i])
                            && isset($params['service_timeout_'.$i])
                            && isset($params['service_alert_'.$i])
                            && (strlen($params['service_name_'.$i]) > 0)
                            && (strlen($params['service_ip_'.$i]) > 0)
                    ) {
                        $enable = (isset($params['service_enable_'.$i]) && $params['service_enable_'.$i] == 1)?true:false;
                        $service = array(
                            'name' => $params['service_name_'.$i],
                            'ip' => $params['service_ip_'.$i],
                            'port' => $params['service_port_'.$i],
                            'timeout' => ((int)$params['service_timeout_'.$i] > 0)?$params['service_timeout_'.$i]:5,
                            'alert' => $params['service_alert_'.$i],
                            'token' => isset($params['service_token_'.$i])?$params['service_token_'.$i]:'',
                            'enable' => $enable
                        );
                        $data['service'][] = $service;
                    }
                }
            }
            //print_r($data); exit;
            $account = LAccount::getInstance();
            $account->saveAccount($data);
            header('location: '.BASE_URL.BS.'index.php?c=message&type=ok&s='.'Account editted !');
        }
        
        $data = $account->loadAccount($ip);
        $layout = LLayout::getInstance();
        $html = $layout->assign('account_edit', array('data' => $data));
        $layout->loadLayout($html);
    }
    
    public function deleteAction()
    {
        $this->checkPermission();
        $ip = isset($_GET['ip'])?$_GET['ip']:null;
        $account = LAccount::getInstance();
        if (!$account->checkExist($ip)) {
            header('location: '.BASE_URL.BS.'index.php?c=message&type=error&s='.'Account doesn\'t exist !');
            exit;
        }
        
        if($account->deleteAccount($ip) == true) {
            header('location: '.BASE_URL.BS.'index.php?c=message&type=ok&s='.'Account deleted !');
            exit;
        } else {
            header('location: '.BASE_URL.BS.'index.php?c=message&type=error&s='.'Account can\'t delete !');
            exit;
        }
    }
}
?>